package com.shopping.mall.core.common.file.controller;

import com.shopping.mall.auth.annotation.UnLogin;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.base.util.OSSUtils;
import com.shopping.mall.core.common.file.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Api(tags = "文件操作")
@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileService fileService;

    @Autowired
    private OSSUtils ossUtils;

    @ApiOperation("上传图片")
    @PostMapping("/photo")
    public Result<String> uploadPhoto(@RequestParam("photo")MultipartFile photo) throws IOException {
        return fileService.uploadPhoto(photo);
    }

    @UnLogin
    @ApiOperation("获取OSS签名")
    @GetMapping("/oss/sign")
    public Result<Object> getOSSSign(@RequestParam String url){
        try {
            return Result.OK(ossUtils.getSignature(url));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return Result.error();
        }
    }
}
